package com.example.myapplication.fagment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.acticity.HomeActivity;
import com.example.myapplication.acticity.Mp3Activity;
import com.example.myapplication.adapter.MyMusicAdapter;
import com.example.myapplication.utils.MediaUtils;
import com.example.myapplication.vo.MobileApplication;
import com.example.myapplication.vo.Mp3Info;
import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class LocalMusicFragment extends Fragment {


    private RecyclerView recyclerView;
    private MyMusicAdapter adapter;
    private ArrayList<Mp3Info> getMp3Infos;
    private HomeActivity mainActivity;
    private ImageView img_album, img_play, img_push, img_next;
    private TextView tex_songName;


    public LocalMusicFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_local_music, container, false);
    }

    /**
     * 拿到主活动的回调上下文
     *
     * @param context
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (HomeActivity) context;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        if (MobileApplication.getInstance().playService != null ){
            setSongName(MobileApplication.getInstance().playService.getCurrentPostion());
            onClick(MobileApplication.getInstance().playService.getCurrentPostion());
        }
    }


    private void initView() {

        recyclerView = getView().findViewById(R.id.re);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        getMp3Infos = MediaUtils.getMp3Infos(getActivity());
        adapter = new MyMusicAdapter(getActivity(), getMp3Infos);
        //通过适配器写好的回调接口获得输入的点击
        adapter.setOnItemClickListener(new MyMusicAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, final int position) {
                MobileApplication.getInstance().playService.play(position);//点击位置播放音乐
                setSongName(position);
                onClick(position);
            }
        });
        recyclerView.setAdapter(adapter);
        tex_songName = getView().findViewById(R.id.song);
        tex_songName.setSelected(true);
        img_album = getView().findViewById(R.id.album);
        img_play = getView().findViewById(R.id.play);
        img_next = getView().findViewById(R.id.next);
        img_push = getView().findViewById(R.id.stop);


    }

    /**
     * 从播放页面返回的时候同步正在播放的音乐和按钮的显示
     */
    @Override
    public void onResume() {
        super.onResume();
        if (MobileApplication.getInstance().playService != null ){
            setSongName(MobileApplication.getInstance().playService.getCurrentPostion());
            onClick(MobileApplication.getInstance().playService.getCurrentPostion());
        }
    }

    /**
     * 如果音乐播放就获得当前播放音乐的歌手和歌名并赋值给底部显示
     */
    public void setSongName(int position){
        if (getMp3Infos != null&&getMp3Infos.size()>0) {
            if (position >= 0 && position < getMp3Infos.size()) {
                Mp3Info mp3Info = getMp3Infos.get(position);
                tex_songName.setText(mp3Info.getArtist()+"-"+mp3Info.getTitle());
            }
        }
    }
    /**
     * 播放按钮，暂停按钮，下一首按钮的实现
     * 开始播放时播放隐藏，暂停按钮出现，暂停时暂停隐藏播放出现
     *
     * @param position
     */
    public void onClick(final int position){
        if (MobileApplication.getInstance().playService.isPause()){
            img_play.setVisibility(View.VISIBLE);
            img_push.setVisibility(View.GONE);
        }else {
            img_push.setVisibility(View.VISIBLE);
            img_play.setVisibility(View.GONE);
        }

        //暂停
        img_push.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                img_play.setVisibility(View.VISIBLE);
                img_push.setVisibility(View.GONE);
                MobileApplication.getInstance().playService.pause();
            }
        });

        //开始
        img_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                img_push.setVisibility(View.VISIBLE);
                img_play.setVisibility(View.GONE);
                if (MobileApplication.getInstance().playService.isPlaying()){
                    MobileApplication.getInstance().playService.start();
                }else {
                    MobileApplication.getInstance().playService.play(position);
                }
            }
        });

        //下一首
        img_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MobileApplication.getInstance().playService.next();
                setSongName(MobileApplication.getInstance().playService.getCurrentPostion());
                img_push.setVisibility(View.VISIBLE);
                img_play.setVisibility(View.GONE);
            }
        });

        tex_songName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Mp3Activity.class);
                startActivity(intent);

            }
        });
    }

    public void changeUiStatus(int position) {
        if (MobileApplication.getInstance().playService != null) {
            if (MobileApplication.getInstance().playService.isPause()) {
                img_play.setVisibility(View.VISIBLE);
                img_push.setVisibility(View.GONE);
            } else {
                img_push.setVisibility(View.VISIBLE);
                img_play.setVisibility(View.GONE);
            }
            setSongName(position);
        }
    }
}